﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using SlamDunk.CMS.BLL;
using SlamDunk.CMS.Model;
using SlamDunk.CMS.Model.DataModel;
using SlamDunk.CMS.UI.Controllers;

namespace SlamDunk.CMS.UI.Areas.Admin.Controllers
{
    /// <summary>
    /// 页面刷新控制器
    /// </summary>
    public class RefreshController : BaseController
    {
        #region 00.实例化业务层对象
        /// <summary>
        /// 文章业务层对象
        /// </summary>
        private readonly ArticleBll ArticleBll = new ArticleBll();
        /// <summary>
        /// 栏目业务层对象
        /// </summary>
        private readonly ChannelBll ChannelBll = new ChannelBll();
        #endregion

        #region 01.加载视图
        /// <summary>
        /// 加载视图
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult Index()
        {
            return this.View();
        }
        #endregion

        #region 02.生成全站
        /// <summary>
        /// 生成全站
        /// </summary>
        /// <returns>生成结果</returns>
        public ActionResult GenerateWholeSite()
        {
            try
            {
                //生成所有内容页
                List<Article> articleList = this.ArticleBll.GetModelList();
                articleList.ForEach(x => this.CreateDetailHtml(x));
                //生成所有聚合页与列表页
                List<Channel> channelList = this.ChannelBll.GetModelList().Where(x => x.Id != 1).ToList();     //去除根目录
                channelList.ForEach(x =>
                {
                    this.CreateIndexHtmlNoRecursion(x);
                    this.CreateListHtml(x);
                });
                //生成首页
                this.CreateHomeHtml();
                return OperateContext.Current.JsonModel(1, "生成全站成功！");
            }
            catch (Exception ex)
            {
                return OperateContext.Current.JsonModel(1, string.Format("生成全站失败，{0}", ex.Message));
            }
        }
        #endregion

        #region 03.生成首页
        /// <summary>
        /// 生成首页
        /// </summary>
        /// <returns>生成结果</returns>
        public ActionResult GenerateHome()
        {
            try
            {
                this.CreateHomeHtml();
                return OperateContext.Current.JsonModel(1, "生成首页成功！");
            }
            catch (ArgumentOutOfRangeException)
            {
                return OperateContext.Current.JsonModel(1, "生成首页失败，首页上的文章不能为空！");
            }
        }
        #endregion

        #region 04.生成聚合页
        /// <summary>
        /// 生成聚合页
        /// </summary>
        /// <param name="id">栏目Id</param>
        /// <returns>生成结果</returns>
        public ActionResult GenerateIndex(int id)
        {
            Channel currentModel = this.ChannelBll.GetModel(id);
            try
            {
                this.CreateIndexHtml(currentModel);
                return OperateContext.Current.JsonModel(1, "生成聚合页成功！");
            }
            catch (Exception ex)
            {
                return OperateContext.Current.JsonModel(1, string.Format("生成聚合页失败，{0}", ex.Message));
            }
        }
        #endregion

        #region 05.生成列表页
        /// <summary>
        /// 生成列表页
        /// </summary>
        /// <param name="id">栏目Id</param>
        /// <returns>生成结果</returns>
        public ActionResult GenerateList(int id)
        {
            Channel currentModel = this.ChannelBll.GetModel(id);
            try
            {
                this.CreateListHtml(currentModel);
                //递归
                List<Channel> channelList = this.ChannelBll.GetModelList(id);
                if (channelList.Count > 0)
                {
                    channelList.ForEach(x => this.GenerateList(x.Id));
                }
                return OperateContext.Current.JsonModel(1, "生成列表页成功！");
            }
            catch (Exception ex)
            {
                return OperateContext.Current.JsonModel(1, string.Format("生成列表页失败，{0}", ex.Message));
            }
        }
        #endregion

        #region 06.生成内容页
        /// <summary>
        /// 生成内容页
        /// </summary>
        /// <param name="id">栏目Id</param>
        /// <returns>生成结果</returns>
        public ActionResult GenerateDetail(int id)
        {
            try
            {
                //获取该栏目下的所有文章
                List<Article> articleList = this.ArticleBll.GetModelList(id);
                //articleList.ForEach(x => base.CreateDetailHtml(x));
                foreach (Article article in articleList)
                {
                    this.CreateDetailHtml(article);
                }
                //递归
                List<Channel> channelList = this.ChannelBll.GetModelList(id);
                channelList.ForEach(x => this.GenerateDetail(x.Id));
                return OperateContext.Current.JsonModel(1, "生成内容页成功！");
            }
            catch (Exception ex)
            {
                return OperateContext.Current.JsonModel(1, string.Format("生成内容页失败，{0}", ex.Message));
            }
        }
        #endregion
    }
}
